/**
 * 
 */
package org.t2.backstage.module.giftcode;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.tool.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.giftcode.model.GiftCodeRandomLog;
import org.t2.backstage.module.giftcode.service.GiftCodeRandomLogService;
import org.t2.backstage.module.giftcode.struct.GiftCodeLogQuery;
import org.t2.backstage.module.giftcode.vo.GiftCodeLogVo;

/**
 * @author YY
 */
@Controller
public class GiftCodeRandomLogController {

	@Autowired
	private GiftCodeRandomLogService giftCodeRandomLogService;
	
	@ApiDocument("请求随机礼包码数据")
	@RequestMapping(value = "/giftCodeRandomLog/data.auth")
	@ResponseBody
	public PageData<GiftCodeLogVo> data(GiftCodeLogQuery query) {
		QuerySet querySet = new QuerySet();
		if (query.getCode() != null && !"".equals(query.getCode())) {
			querySet.addCondition("code like ?", "%" + query.getCode() + "%");
		}
		if (query.getPlayerName() != null && !"".equals(query.getPlayerName())) {
			querySet.addCondition("player_name like ?", "%" + query.getPlayerName() + "%");
		}
		if (query.getPlayerId() > 0) {
			querySet.addCondition("player_id = ?", query.getPlayerId());
		}
		querySet.limit(query.getPage(), query.getLimit());
		querySet.orderBy("order by id desc");
		querySet.formWhere();
		
		PageData<GiftCodeRandomLog> logData = giftCodeRandomLogService.query(querySet);
		
		PageData<GiftCodeLogVo> pageData = new PageData<>();
		for(GiftCodeRandomLog log : logData.getData()) {
			GiftCodeLogVo vo = new GiftCodeLogVo();
			vo.setId(log.getId());
			vo.setGiftId(log.getGiftId());
			vo.setCode(log.getCode());
			vo.setPlayerId(log.getPlayerId());
			vo.setPlayerName(log.getPlayerName());
			if(log.getPlayerId() > 0) {
				vo.setServer(log.getServerId() + "-" + log.getServerName());
				vo.setUseTime(DateTimeUtil.formatMillis(DateTimeUtil.YYYY_MM_DD_HH_MM_SS, log.getUseTime()));
			}
			pageData.getData().add(vo);
		}
		pageData.setCount(logData.getCount());
		return pageData;
	}
}
